Skip to content

fix: handle JSONDecodeError when parsing JSON tensors in model output conversion#221

Open
amathxbt wants to merge 3 commits intoOpenGradient:mainfrom
amathxbt:fix/json-tensor-parse-crash
Open

fix: handle JSONDecodeError when parsing JSON tensors in model output conversion#221
amathxbt wants to merge 3 commits intoOpenGradient:mainfrom
amathxbt:fix/json-tensor-parse-crash

Conversation

@amathxbt
Copy link
Copy Markdown
Contributor

🐛 Critical Bug: Unhandled JSONDecodeError Crashes Entire Model Output Parsing

File: src/opengradient/client/_conversions.py

What's broken

In both convert_to_model_output() (line 159) and convert_array_to_model_output() (line 206), JSON tensor values from the blockchain are parsed with bare json.loads() calls — with no error handling:

# convert_to_model_output — line 159
output_dict[name] = np.array(json.loads(value))  # ← unguarded

# convert_array_to_model_output — line 206  
json_data[name] = np.array(json.loads(value))     # ← unguarded

If any single JSON tensor in the response contains malformed data (encoding corruption, contract bug, partial write), json.loads() raises an unhandled json.JSONDecodeError that:

  1. Crashes the entire output parsing — all other valid tensors are lost
  2. Provides zero diagnostic context (which tensor failed, what the value was)
  3. Surfaces as a raw Python traceback rather than a meaningful SDK error

Fix

Wrap each json.loads() call in a try/except and log a detailed warning instead of crashing:

try:
    output_dict[name] = np.array(json.loads(value))
except (json.JSONDecodeError, TypeError, ValueError) as e:
    logging.warning(f"Failed to parse JSON tensor '{name}': {e} (raw value: {value!r})")

Applied to both conversion functions.

Impact

Before After
One malformed JSON tensor crashes entire model output Bad tensor is skipped with a warning; all valid tensors preserved
No context in traceback Warning includes tensor name and raw value for debugging
SDK crashes with raw Python exception Graceful degradation

Files Changed

  • src/opengradient/client/_conversions.py

@amathxbt
Copy link
Copy Markdown
Contributor Author

amathxbt commented Mar 31, 2026

@adambalogh can you take quick action is something critical
Thanks 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant